package studychinese.fragment;


import android.content.res.TypedArray;
import android.os.Bundle;
import android.support.v4.app.Fragment;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.AdapterView;
import android.widget.GridView;
import android.widget.TextView;

import com.hschinese.pronunciation.R;

import java.util.ArrayList;
import java.util.List;
import java.util.Map;

import butterknife.Bind;
import butterknife.ButterKnife;
import studychinese.adapters.NativeCountryAdapter;
import studychinese.model.RegisterBean;
import studychinese.model.UserBean;

/**
 * A simple {@link Fragment} subclass.
 */
public class NativeCityFragment extends BaseFragment  {


    @Bind(R.id.tv_native_titile)
    TextView tvNativeTitile;
    @Bind(R.id.gv_native_city)
    GridView gvNativeCity;
    private NativeCountryAdapter adapter;


    public static NativeCityFragment newInstance(String title) {

        Bundle args = new Bundle();
        args.putString("title", title);
        NativeCityFragment fragment = new NativeCityFragment();
        fragment.setArguments(args);
        return fragment;
    }



    @Override
    protected View initView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
        View view = inflater.inflate(R.layout.fragment_native_city, container, false);
        ButterKnife.bind(this, view);
        init();
        return view;
    }

    private void init() {
      final List<Integer> list=  new ArrayList<>();
      final List<String> listName=  new ArrayList<>();

        Map<String, Object> map;
        TypedArray ta = getResources().obtainTypedArray(R.array.feed_icons);
        String[] titleArr = getResources().getStringArray(R.array.feed_names);
        for(int i=0; i<titleArr.length; i++)
        {

            list.add(ta.getResourceId(i, 0));
            listName.add(titleArr[i]);

        }

        adapter = new NativeCountryAdapter(list,getActivity());
        gvNativeCity.setAdapter(adapter);
        gvNativeCity.setOnItemClickListener(new AdapterView.OnItemClickListener() {
            @Override
            public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
                RegisterBean.DataBean infor = UserBean.getInfor();

                infor.setCountry_id(listName.get(position));
                UserBean.putUserBean(infor);
                adapter.update(position,gvNativeCity);
                adapter.notifyDataSetChanged();

            }
        });
        if(listName.size()>0){
            RegisterBean.DataBean infor = UserBean.getInfor();
            infor.setCountry_id(listName.get(0));
        }

    }


    @Override
    public void onDestroyView() {
        super.onDestroyView();

        ButterKnife.unbind(this);
    }


}
